# 记账本项目 - 深度清理脚本
Write-Host "========================================" -ForegroundColor Green
Write-Host "记账本项目深度清理工具" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""

# 停止 Gradle 守护进程
Write-Host "正在停止 Gradle 守护进程..." -ForegroundColor Yellow
try {
    & gradle --stop
    Write-Host "Gradle 守护进程已停止" -ForegroundColor Green
} catch {
    Write-Host "Gradle 守护进程停止失败或未运行" -ForegroundColor Yellow
}

# 清理 Gradle 缓存
Write-Host ""
Write-Host "正在清理 Gradle 缓存..." -ForegroundColor Yellow
$gradleCachePath = "$env:USERPROFILE\.gradle\caches"
if (Test-Path $gradleCachePath) {
    Remove-Item -Path $gradleCachePath -Recurse -Force
    Write-Host "Gradle 缓存已清理" -ForegroundColor Green
} else {
    Write-Host "Gradle 缓存目录不存在" -ForegroundColor Yellow
}

# 清理项目构建目录
Write-Host ""
Write-Host "正在清理项目构建目录..." -ForegroundColor Yellow
$buildPaths = @("build", "app\build", ".gradle")
foreach ($path in $buildPaths) {
    if (Test-Path $path) {
        Remove-Item -Path $path -Recurse -Force
        Write-Host "已清理: $path" -ForegroundColor Green
    } else {
        Write-Host "目录不存在: $path" -ForegroundColor Yellow
    }
}

# 清理 Android Studio 缓存
Write-Host ""
Write-Host "正在清理 Android Studio 缓存..." -ForegroundColor Yellow
$studioCachePaths = @(
    "$env:USERPROFILE\.android",
    "$env:USERPROFILE\.AndroidStudio*",
    "$env:LOCALAPPDATA\Google\AndroidStudio*"
)
foreach ($path in $studioCachePaths) {
    if (Test-Path $path) {
        Write-Host "发现 Android Studio 缓存: $path" -ForegroundColor Yellow
        Write-Host "建议手动清理此目录（如果遇到问题）" -ForegroundColor Yellow
    }
}

# 验证清理结果
Write-Host ""
Write-Host "正在验证清理结果..." -ForegroundColor Yellow
$remainingPaths = @("build", "app\build", ".gradle")
$allCleaned = $true
foreach ($path in $remainingPaths) {
    if (Test-Path $path) {
        Write-Host "警告: 仍有残留文件: $path" -ForegroundColor Red
        $allCleaned = $false
    }
}

if ($allCleaned) {
    Write-Host "所有项目构建文件已清理完成" -ForegroundColor Green
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "清理完成！" -ForegroundColor Green
Write-Host "建议在 Android Studio 中执行: File -> Invalidate Caches and Restart" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Green

Read-Host "按回车键继续" 